# -*- coding:utf-8 -*-
"""
@author:zhuyuping
@description:
@time:2020/5/24 9:20
"""

from django import forms
from django.contrib.auth.models import User

from userprofile.models import Profile


class UserLoginForm(forms.Form):
    username = forms.CharField()
    password = forms.CharField()


class UserRegisterForm(forms.ModelForm):  # 对数据库进行操作的表单应继承forms.ModelForm
    # 复写 User 的密码
    password = forms.CharField()
    password2 = forms.CharField()

    class Meta:
        model = User
        fields = ('username', 'email')
        # fields = '__all__'
        # exclude = ()

    def clean_password2(self):
        # def clean_[字段]这种写法Django会自动调用，来对单个字段的数据进行验证清洗。
        data = self.cleaned_data
        if data.get('password') == data.get('password2'):
            return data.get('password')
        else:
            raise forms.ValidationError('密码输入不一致，请重试')


class ProfileForm(forms.ModelForm):
    class Meta:
        model = Profile
        fields = ('phone', 'avatar', 'bio')
